# coding: utf-8
from PySide6.QtCore import QSize, Qt
from PySide6.QtWidgets import QVBoxLayout, QListWidgetItem

from components import TagListWidget, GridLayout
from server.api import BirdAPI, birdClass
from ..base import BaseInterface, NullDataWidget


class BirdInterface(BaseInterface):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.totalPage = 100
        self.currentPage = 0
        self.cids = None
        self.tag = ''

        self.verticalLayout = QVBoxLayout(self.view)
        self.classListWidget = TagListWidget(self.view)
        self.tagsListWidget = TagListWidget(self.view)
        self.waterfallLayout = GridLayout()
        self.nullDataWidget = NullDataWidget(self.view)
        self.__initWidgets()
        self.__initLayout()

        self.connectSignals()

    def __initWidgets(self):
        self.setObjectName('BirdInterface')

        self.nullDataWidget.setVisible(False)

        self.classListWidget.setTag('分类：')
        self.tagsListWidget.setTag('标签：')

        for data in birdClass():
            data: dict
            item = QListWidgetItem(data['name'])
            item.setSizeHint(QSize(80, 40))
            item.setData(Qt.ItemDataRole.UserRole, data['cids'])
            item.setData(Qt.ItemDataRole.UserRole + 1, data.get('tags', []))
            item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
            self.classListWidget.addItem(item)
        self.classListWidget.setCurrentRow(0)
        self.onClassSelected()

    def __initLayout(self):
        self.waterfallLayout.setContentsMargins(0, 0, 0, 0)
        self.waterfallLayout.setSpacing(10)
        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.addWidget(self.classListWidget, 0, Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.addWidget(self.tagsListWidget, 0, Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.addLayout(self.waterfallLayout)
        self.verticalLayout.addWidget(self.nullDataWidget, 1)
        self.verticalLayout.addStretch(1)

    def connectSignals(self):
        self.classListWidget.itemSelectionChanged.connect(self.onClassSelected)
        self.tagsListWidget.itemSelectionChanged.connect(self.onTagSelected)

    def onClassSelected(self):
        self.tagsListWidget.clear()
        self.clear()
        self.tag = ''
        item = self.classListWidget.currentItem()
        self.cids = item.data(Qt.ItemDataRole.UserRole)
        tags = item.data(Qt.ItemDataRole.UserRole + 1)
        for tag in tags:
            item = QListWidgetItem(tag)
            item.setSizeHint(QSize(80, 40))
            item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
            self.tagsListWidget.addItem(item)
        if not tags:
            self.tagsListWidget.setVisible(False)
        else:
            self.tagsListWidget.setVisible(True)

        self.load(BirdAPI.getListByCategory, self.cids, slot=self.onCategoryLoaded)

    def onCategoryLoaded(self, result: dict):
        data = result['data']
        dataList = data['list']
        self.totalPage = data['total_page']
        if not dataList:
            if self.currentPage <= 1:
                self.nullDataWidget.setText('没有找到相关的壁纸')
                self.nullDataWidget.setVisible(True)
            else:
                self.nullDataWidget.setText('没有更多的壁纸了')
            self.waterfallLayout.update()
            return
        for item in dataList:
            url = item['url']
            tag = item['tag']
            category = item['category']
            label = self.addLabel(url, self.waterfallLayout)
            label.setTitle(tag)
            label.setMaxUrl(url)
            label.setDescription(category)
        self.lazyTimerStart()

    def onTagSelected(self):
        item = self.tagsListWidget.currentItem()
        self.tag = item.text()
        self.cids = None
        self.clear()
        self.load(BirdAPI.getListByHotTag, self.tag, slot=self.onCategoryLoaded)

    def nextPage(self):
        if self.currentPage < self.totalPage:
            self.currentPage += 1
            self.loadData()
        else:
            self.nullDataWidget.setVisible(True)
            self.nullDataWidget.setText('没有更多的壁纸了')

    def clear(self):
        self.waterfallLayout.takeAllWidgets()
        self.lazyWidgets.clear()

    def onReload(self):
        super().onReload()
        self.clear()
        self.currentPage = 1
        self.loadData()

    def loadData(self):
        if self.cids:
            self.load(BirdAPI.getListByCategory, self.cids, page=self.currentPage, slot=self.onCategoryLoaded)
        else:
            self.load(BirdAPI.getListByHotTag, self.tag, page=self.currentPage, slot=self.onCategoryLoaded)
